package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.pojo.dto.BrandInsertDto;
import cn.tedu.csmall.product.pojo.dto.CategoryUpdateDto;
import cn.tedu.csmall.product.pojo.vo.BrandListItemVo;
import cn.tedu.csmall.product.service.BrandService;
import cn.tedu.csmall.product.web.JsonResult;
import cn.tedu.csmall.product.web.ServiceCode;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/brand")
@Api(tags = "03. 品牌管理模块")
public class BrandController {

    @Autowired
    BrandService service;
    @GetMapping("/add-new")
    public JsonResult<Void> addNew(BrandInsertDto brandInsertDto) {
        service.insert(brandInsertDto);
        return JsonResult.ok();
    }


    @GetMapping("")
    public JsonResult<List<BrandListItemVo>> list() {
        List<BrandListItemVo> list = service.list();
        return JsonResult.ok(list);
    }

    //    http://localhost:8080/admin/4/disable
    @PostMapping("/{id:[0-9]+}/disable")
    @ApiOperation("管理员禁用")
//    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @ApiOperationSupport(order = 20)
    public JsonResult<Void> disableEnable(@PathVariable @Range(min = 1)Long id){
        service.disable(id);
        return JsonResult.ok();
    }

    //    http://localhost:8080/admin/2/enable
    @PostMapping("/{id:[0-9]+}/enable")
    @ApiOperation("管理员启用")
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @ApiOperationSupport(order = 30)
    public JsonResult<Void> Enable(@PathVariable @Range(min = 1)Long id){
        service.enable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除管理员")
    @ApiOperationSupport(order = 200)
    @PreAuthorize("hasAuthority('/ams/admin/delete')")
    public JsonResult<Void> delete(@PathVariable @Range(min = 1) Long id){
        service.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改类别详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id,
                                           @Valid CategoryUpdateDto categoryUpdateDTO) {
        service.updateInfoById(id, categoryUpdateDTO);
        return JsonResult.ok();
    }

}
